/*
 * @Descripttion: 胡腾辉 | feico
 * @version: V1.0
 * @Author: feico
 * @Date: 2022-02-22 14:46:27
 * @LastEditors: feico
 * @LastEditTime: 2022-02-22 15:47:26
 */
import React from 'react'
import {Route,Redirect,useLocation} from 'react-router-dom'
import {isAuth} from '../utils'

export default function PrivateRoute({children,component:Component,...rest}) {
    const location = useLocation()
        return (
        <Route 
        {...rest}
        render={()=>{
            if(isAuth()){
                return children || <Component/>
            }else{
               
                return (
                    <Redirect
                    to={{
                        // #1 指定跳转地址
                        pathname: '/login',
                        // #2 通过 state 来传递额外的参数
                        state: {
                            // 把当前的地址作为 from 属性传递了过去
                            from: location.pathname,
                        },
                    }}
                />)
            }
        }}
        />
            )
    }